﻿#region Copyright Notice

//Copyright © 2007-2011, PARROT SA, all rights reserved. 

//DISCLAIMER 
//The APIs is provided by PARROT and contributors "AS IS" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability 
//and fitness for a particular purpose are disclaimed. In no event shall PARROT and contributors be liable for any direct, indirect, incidental, special, exemplary, or 
//consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however 
//caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this 
//software, even if advised of the possibility of such damage. 

//Author            : Wilke Jansoone
//Email             : wilke.jansoone@digitude.net
//Publishing date   : 28/11/2010 

//Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions
//are met:
//    - Redistributions of source code must retain the above copyright notice, this list of conditions, the disclaimer and the original author of the source code.
//    - Neither the name of the PixVillage Team, nor the names of its contributors may be used to endorse or promote products derived from this software without 
//      specific prior written permission.

#endregion

#region Imports

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Net.NetworkInformation;

#endregion

namespace Wilke.Interactive.Drone.Control
{
    internal static class NetworkHelper
    {
        internal static UdpClient CreateUdpSocket(string ipString, int port, int timeOut)
        {

            UdpClient udpClient = null;

            try
            {
                udpClient = CreateUdpSocket(ipString, port);
                udpClient.Client.ReceiveTimeout = timeOut;
                udpClient.Client.SendTimeout = timeOut;
            }
            catch
            {
                throw;
            }

            return udpClient;
        }

        internal static UdpClient CreateUdpSocket(string ipString, int port)
        {
            UdpClient udpClient = null;

            try
            {
                IPAddress ipLocalAddress = IPAddress.Parse(ipString);
                IPEndPoint ipLocalEndPoint = new IPEndPoint(ipLocalAddress, port);
                udpClient = new UdpClient(ipLocalEndPoint);                
            }
            catch
            {
                throw;
            }

            return udpClient;
        }

        internal static TcpClient CreateTcpSocket(string ipString, int port, int timeOut)
        {

            TcpClient tcpClient = null;

            try
            {
                tcpClient = CreateTcpSocket(ipString, port);
                tcpClient.Client.ReceiveTimeout = timeOut;
                tcpClient.Client.SendTimeout = timeOut;
            }
            catch
            {
                throw;
            }

            return tcpClient;
        }

        internal static TcpClient CreateTcpSocket(string ipString, int port)
        {
            TcpClient tcpClient = null;

            try
            {
                IPAddress ipLocalAddress = IPAddress.Parse(ipString);
                IPEndPoint ipLocalEndPoint = new IPEndPoint(ipLocalAddress, port);
                tcpClient = new TcpClient(ipLocalEndPoint);
            }
            catch
            {
                throw;
            }

            return tcpClient;
        }

        internal static IPEndPoint CreateRemoteEndPoint(string remoteIPString, int port)
        {
            IPEndPoint ipEndPoint = null;

            try
            {
                IPAddress ipAddress = IPAddress.Parse(remoteIPString);
                ipEndPoint = new IPEndPoint(ipAddress, port);
            }
            catch { }

            return ipEndPoint;
        }

        internal static void CloseUdpConnection(UdpClient socket)
        {
            if (socket != null)
            {
                socket.Close();
            }
        }

        internal static void CloseTcpConnection(TcpClient socket)
        {
            if (socket != null)
            {
                socket.Close();
            }
        }

        internal static WlanInterface GetWirelessNetworkInterface()
        {
            WlanClient wlanClient = new WlanClient();
            WlanInterface wlanselectedWlanInterface = null;

            foreach (WlanInterface wlanInterface in wlanClient.Interfaces)
            {
                if (wlanInterface.NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    wlanselectedWlanInterface = wlanInterface;
                }
            }

            return wlanselectedWlanInterface;
        }

        internal static string GetWirelessNetworkAddress()
        {
            string ipString = null;

            try
            {
                WlanInterface wlanInterface = NetworkHelper.GetWirelessNetworkInterface();
                if (wlanInterface.InterfaceState == Wlan.WlanInterfaceState.Connected)
                {
                    ipString = wlanInterface.NetworkInterface.GetIPProperties().UnicastAddresses[1].Address.ToString();
                }
            }
            catch (Exception)
            {

            }

            return ipString;
        }      
    }
}
